package com.gmail.bloim.serg.t1006;

import java.io.*;
import java.util.Arrays;
import java.util.LinkedList;

import static com.gmail.bloim.serg.t1006.Coords.move;

/**
 * User: Администратор
 * Date: 27.04.13
 * Time: 17:19
 */
public class Program {
    private boolean test = System.getProperty("ONLINE_JUDGE") == null;
    public Contour[] contourImage = new Contour[1000];
    public char[] image;
    private LinkedList<Integer> points;

    public static void main(String[] args) throws Exception {
        new Program().run();
    }

    private void run() throws IOException {
        init();
        readInput();
        solve();
        System.out.flush();
    }

    private void init() {
    }

    private void solve() {

        trackContours();
    }

    private void trackContours() {
        LinkedList<Integer> points = new LinkedList<Integer>();
        for (int i = 0; i < image.length; i++) {
            char c = image[i];
            if ('.' != c)
                points.add(i);
        }
        if (points.size() == 0)
            return;

        while (!points.isEmpty()) {
            int cind = points.getLast();
            Contour contour = trackContour(cind);
            points.removeAll(contour.getPoints());
            for (Integer point : contour.getPoints()) {
                contourImage[point] = contour;
            }
        }

    }

    private Contour trackContour(int startCind) {
        char ch = getChar(startCind);
        Symbol startSymbol = Symbol.getByChar(ch);
        Direction dir = startSymbol.getDefaultOut();
        Contour contour = new Contour();
        contour.addPoint(startCind);
        int end = traceContourByDirection(contour, startCind, dir);
        if (end == startCind) {
            contour.setCycled(true);
            return contour;
        }
        int firstBreakPoint = end;
        int secondBreakPoint = traceContourByDirection(contour, startCind, startSymbol.getSecondaryOut());

        contour.addDependancy(firstBreakPoint);
        contour.addDependancy(secondBreakPoint);
        return contour;
    }

    private int traceContourByDirection(Contour contour, int startCind, Direction dir) {
        int nextCind = startCind;
        while (true) {
            nextCind = move(nextCind, dir);
            if (nextCind == startCind) {//cycle contour
                return startCind;
            }
            char c = getChar(nextCind);
            Symbol symbol = Symbol.getByChar(c);
            dir = symbol.forward(dir);
            if (dir == null) { //Reach contour break
                return nextCind;
            }
            contour.addPoint(nextCind);
        }
    }

    private char getChar(int cind) {
        return image[cind];
    }

    private char getChar(int x, int y) {
        return image[50 * y + x];
    }


    private void readInput() throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in,"ISO-8859-1"));
        char[] image = new char[20 * 50];
        for (int i = 0; i < 20; i++) {
            bufferedReader.read(image, i * 50, 50);
            char eol = (char) bufferedReader.read();
            if (eol == '\n')
                continue;
            bufferedReader.read();
        }
        this.image = image;
        if (test) {
            int ind = 0;
            for (int i = 0; i < 20; i++) {
                for (int j = 0; j < 50; j++) {
                    System.out.print(image[ind++]);
                }
                System.out.print('\n');
            }
        }
    }
}
